<template>
  <div>
    <el-card class="box-card" :body-style="{ padding: '15px',height:'150px'}">
      <el-form :inline="true" :model="keyword" ref="keyword" class="demo-form-inline">
        <el-form-item></el-form-item>
        <el-form-item label="采购编号" prop="orderName">
          <el-input clearable v-model="keyword.orderName" placeholder="请输入采购编号"></el-input>
        </el-form-item>
        <el-form-item label="采购日期" prop="orderTime" style="margin-left: 30px;">
          <el-date-picker
            v-model="this.keyword.orderTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        
        <el-form-item label="到货日期" prop="arrivalTime" style="margin-left: 12px;">
          <el-date-picker
            v-model="this.keyword.orderTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        
          <el-form-item label="确认收货人" prop="arrivalConfirmer">
            <el-select v-model="keyword.arrivalConfirmer" placeholder="收货人" >
              <el-option v-for="name in nameList" :label="name.username" :value="name.username"></el-option>
            </el-select>
          </el-form-item>
        

        <el-form-item style="float: right;">
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button @click="resetForm('keyword')" icon="el-icon-refresh-right">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card box-card2">
      <div style="margin-top: -15px;margin-bottom: 10px;">

        <el-button type="danger" icon="el-icon-delete" @click="delBatch">批量删除</el-button>
      </div>
      
      <!-- 详情框 -->
      <el-dialog title="详情信息" :visible.sync="dialogTableVisible">
        <el-table
          :data="orderDetail"
          border
          width="500px"
          :cell-style="{ textAlign: 'center' }"
          :header-cell-style="{ textAlign: 'center' }"
        >
          <el-table-column prop="orderName" label="采购名称" width="180"></el-table-column>
          <el-table-column property="brand" label="品牌" width="150"></el-table-column>
          <el-table-column property="spe" label="规格型号" width="150"></el-table-column>
          <el-table-column property="unit" label="单位"></el-table-column>
          <el-table-column property="quantity" label="采购数量"></el-table-column>
          <el-table-column property="uquantity" label="未到数量"></el-table-column>
          <el-table-column property="aquantity" label="已到数量"></el-table-column>
          <el-table-column property="userItem" label="计划使用项目" width="180px"></el-table-column>
          <el-table-column property="ware" label="存入仓库" width="180px"></el-table-column>
          <el-table-column label="货物图片" width="180px">
            <img :src="picS" width="100px" height="100px"/>
          </el-table-column>

        </el-table>
      </el-dialog>
      <el-table
        v-loading="loading"
        :data=" orderList"
        height="370"
        border
        class="table1"
        :cell-style="{ textAlign: 'center' }"
        :header-cell-style="{ textAlign: 'center' }"
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="orderUid" label="采购编号" :show-overflow-tooltip="true" width="200"></el-table-column>
        <el-table-column prop="orderTime" label="采购日期" width="160"></el-table-column>
        <el-table-column prop="arrivalTime" label="预计到货日期" width="160"></el-table-column>
        <el-table-column prop="arrivalConfirmer" label="确认收货人" width="150"></el-table-column>
        <el-table-column prop="arrivalStatus" width="130" label="到货状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.arrivalStatus=='1'?'blue':scope.row.arrivalStatus=='2'?'danger':scope.row.arrivalStatus=='3'?'warning':'info'"
            >{{scope.row.arrivalStatus=='1'?'全部到货':scope.row.arrivalStatus=='2'?'未到货':scope.row.arrivalStatus=='3'?'部分到货':'未知'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" width="180" label="创建日期"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="details(scope.row)">详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
            <el-button size="mini" type="text"      icon="el-icon-s-claim" @click="entry(scope.row)">入库</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  
   
    <el-card class="box-card" :body-style="{ padding: '15px',height:'50px'}">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[ 5,10,15,20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getList,
  delOrderById,
  delBatch,
  detail,
  getName,
  entry
} from "@/api/arrival.js";
export default {
  name: "PO",
  data() {
    return {
      // loading: true,
      orderList: [],
      orderDetail: [],
      nameList: [],
      keyword: {
        orderUid: "",
        orderTime: "",
        arrivalTime: "",
        arrivalConfirmer:"",
        username:"",
      },
      loading: true,
     
      order: {
        pic: null,
        orderId: null,
        orderUid: null,
        orderName: null,
        brand: null,
        spe: null,
        unit: null,
        ware: null,
        quantity: null,
        uquantity: null,
        aquantity: null,
        orderTime: null,
        arrivalTime: null,
        cargoPic: null,
        userItem: null,
        arrivalConfirmer:null,
        deliveryTime:null
      },
      orderX: {
        pic: null,
        orderId: null,
        orderUid: null,
        orderName: null,
        brand: null,
        spe: null,
        unit: null,
        ware: null,
        quantity: null,
        uquantity: null,
        aquantity: null,
        orderTime: null,
        arrivalTime: null,
        cargoPic: null,
        userItem: null,
       
      },
      picS: "",
      total: 0,
      currentPage: 1, // 当前页面
      total: 0, // 总条数
      pageNum: 1, // 当前页面
      pageSize: 5, // 每页大小
      dialogTableVisible: false, // 详情

      idList: [],
      url: "https://xiaoze-taitai.oss-cn-beijing.aliyuncs.com/"
    };
  },
  methods: {
    // 入库
    entry(row){
      this.$confirm("确认入库吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.order = row
        entry(this.order).then(res=>{
          
          if(res.code==20000 && res.data !=false){
            this.$message({
            type: 'success',
            message: '入库成功!'
          });
          }else if(res.data == false){
            this.$message({
              type: 'error',
              message: '已经入库，无需重复入库!'
            });
          }
         
          
          this.findAll()
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消"
        })
      });
    },
    Recancle(forname){
      this.$refs[forname].resetFields()
      this.dialogReceVisible = false
    },
    
    // 取消默认清空
    cancle(froname) {
      this.$refs[froname].resetFields();
      this.dialogFormVisible = false;
      this.fileSrc = "";
    },
    handleChange(file) {
      this.file = file.raw;
    },

  
    findAll() {
      getList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        orderName:this.keyword.orderName,
        arrivalConfirmer:this.keyword.arrivalConfirmer
       }).then(res => {
        console.log(res);
        this.orderList = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.findAll();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.findAll();
    },
    // 新增
  
    
   
    // 详情
    details(row) {
      this.dialogTableVisible = true;
      this.orderX = row;
      detail({
        id: this.orderX.orderId
      }).then(res => {
        console.log(res.data);
        this.orderDetail = res.data;
        console.log(res.data[0].cargoPic);
        this.picS=this.url+res.data[0].cargoPic
        console.log(this.picS);
      });
    },
   
    // 根据id删除
    handleDelete(index, row) {
      console.log(row.orderId);
      this.$confirm("此操作将永久删除该订单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delOrderById({
          id: row.orderId
        })
          .then(res => {
            if (res.code == 20000) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1000
              });
              this.findAll();
            }
          })
          .catch(err => {
            this.$message({
              message: "删除失败",
              type: "error",
              duration: 1000
            });
          });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消"
        })
      });
    },
    handleSelectionChange(val) {
      this.idList = [];
      this.multipleSelection = val;
      for (var i = 0; i < this.multipleSelection.length; i++) {
        let user = this.multipleSelection[i];
        this.idList.push(user.orderId);
      }
    },
    // 批量删除
    delBatch() {
      this.$confirm("是否删除选中的订单？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const idString = this.idList.join(",");
        delBatch({
          ids: idString
        })
          .then(res => {
            this.$message({
              message: "删除成功",
              type: "success",
              duration: 1000
            });
            this.findAll();
          })
          .catch(err => {
            this.$message({
              message: "删除失败",
              type: "error",
              duration: 1000
            });
          });
      });
    },
    // 重置input框
    resetForm(fromname) {
      
      this.$refs[fromname].resetFields();
      this.findAll();
    },
    search() {
      this.findAll();
    },
    findUserName(){
      getName().then(res=>{
        console.log("nameList",res.data);
        this.nameList=res.data
      })
    }
  },

  mounted() {
    this.findAll();
    this.findUserName()
   
  }
};
</script>

<style>
.box-card {
  width: 100%;
  margin-top: 10px;
}
.box-card3 {
  height: 100px;
}


.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  margin-left: 80px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>